// pages/user/tixian/index.js
import axios from "../../../utils/axios"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading:false,
    amount:undefined,
    total:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.companyId = wx.getStorageSync('company_id');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  async cashout(){
    if(this.data.loading){
      return;
    }
    if(this.data.amount == "" || this.data.amount == 0 || !this.data.amount){
      wx.showToast({
        title: '请输入提现金额',
        icon:'error'
      })
      return;
    }
    const total = await this.getAmount();
    if(this.data.amount > total){
      wx.showToast({
        title: '可提现余额不足！',
        icon:"none"
      })
      return;
    }
    this.setData({
      loading:true
    })
    axios.post("/app/cashOut",{
      balance:this.data.amount*1,
      companyId:this.companyId,
    }).then(res=>{
      wx.showToast({
        title: res.msg,
        icon:'none'
      })
      setTimeout(()=>{
        wx.redirectTo({
          url: '/pages/user/cashBill/index',
        })
      },1200)
    }).finally(()=>{
      this.setData({
        loading:false
      })
    })
  },
  inputChange(e){
    let amount = e.detail.value
    if(amount > this.data.total){
      amount = this.data.total;
    }
    this.setData({
      amount
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getAmount();
  },
  async getAmount(){
    const res = await axios.get("/app/getAmount",{
        companyId:this.companyId,
      }).then(res=>{
          this.setData({
            total:res.amount
          })
          return res.amount
      })
      return res;
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})